package com.bootdo.common.utils;

import com.bootdo.common.domain.FileDO;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class QrcodeUtil {


    /**
     * 生成二维码
     *
     * @param userId   信息
     * @param filePath 二维码保存目录
     * @return 二维码保存后的路径
     */
    public static String generateQrcode(String userId, String filePath) throws UnsupportedEncodingException {
        String fileUrl = null;
        String fileName = String.valueOf(System.currentTimeMillis()) + ".jpg";
        String content = AuthUtil.LOCALHOST + "/wx/weixinAutoLogin?userId=" + userId;
        int width = 300; // 图像宽度
        int height = 300; // 图像高度
        String format = "jpg";// 图像类型
        Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>();
        hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
        BitMatrix bitMatrix = null;// 生成矩阵

        try {
            File file = new File(filePath, fileName);
            File parentPath = new File(file.getParent());
            if (!parentPath.exists()) {
                parentPath.mkdirs();
            }
            if (!file.exists()) {
                file.createNewFile();
            }
            bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints);
            Path path = FileSystems.getDefault().getPath(filePath, fileName);
            MatrixToImageWriter.writeToPath(bitMatrix, format, path);// 输出图像
            fileUrl = filePath + File.separator + fileName;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return fileName;
    }

    /**
     * 二维码和背景合并
     *
     * @param x        二维码在背景图的x轴坐标(350)
     * @param y        二维码在背景图的y轴坐标(105)
     * @param qrPath   二维码图片路径()
     * @param bgPath   背景图片路径()
     * @param descPath 合并图片存放路径
     * @return 合并后的图片地址
     */
    public static void mergeImage(int x, int y, String qrPath, String bgPath, String descPath) {
        try {
            //读取第一张图片
            File fileOne = new File(bgPath);
            BufferedImage ImageOne = ImageIO.read(fileOne);
            int width = ImageOne.getWidth();//图片宽度
            int height = ImageOne.getHeight();//图片高度
            //从图片中读取RGB
            int[] ImageArrayOne = new int[width * height];
            ImageArrayOne = ImageOne.getRGB(0, 0, width, height, ImageArrayOne, 0, width);
            //对第二张图片做相同的处理
            File fileTwo = new File(qrPath);
            BufferedImage ImageTwo = ImageIO.read(fileTwo);
            int widthTwo = ImageTwo.getWidth();//图片宽度
            int heightTwo = ImageTwo.getHeight();//图片高度
            int[] ImageArrayTwo = new int[widthTwo * heightTwo];
            ImageArrayTwo = ImageTwo.getRGB(0, 0, widthTwo, heightTwo, ImageArrayTwo, 0, widthTwo);

            //生成新图片
            BufferedImage ImageNew = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
            ImageNew.setRGB(0, 0, width, height, ImageArrayOne, 0, width);//设置左半部分的RGB
            ImageNew.setRGB(x, y, widthTwo, heightTwo, ImageArrayTwo, 0, widthTwo);//设置右半部分的RGB
            ImageNew.setRGB(x, y, widthTwo, heightTwo, ImageArrayTwo, 0, widthTwo);//设置右半部分的RGB
            File outFile = new File(descPath);
            File parentPath = new File(outFile.getParent());
            if (!parentPath.exists()) {
                parentPath.mkdirs();
            }
            if (!outFile.exists()) {
                outFile.createNewFile();
            }
            ImageIO.write(ImageNew, "png", outFile);//写图片
            outFile.getAbsolutePath();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 生成微信支付二维码
     *
     * @param payUrl
     * @param path
     */
    public static void generateWxPayQrCode(String payUrl, String path) {
        String filePath = path;
        int width = 300; // 图像宽度
        int height = 300; // 图像高度
        String format = "jpg";// 图像类型
        Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>();
        hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
        BitMatrix bitMatrix = null;// 生成矩阵

        try {
            File file = new File(path);
            File parentPath = new File(file.getParent());
            if (!parentPath.exists()) {
                parentPath.mkdirs();
            }
            if (!file.exists()) {
                file.createNewFile();
            }
            bitMatrix = new MultiFormatWriter().encode(payUrl, BarcodeFormat.QR_CODE, width, height, hints);
            Path fullPath = FileSystems.getDefault().getPath(filePath);
            MatrixToImageWriter.writeToPath(bitMatrix, format, fullPath);// 输出图像
        } catch (WriterException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
